package com.creative.accountsdk.teacher;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.R;
import com.creative.accountsdk.ThirdLoginActivity;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.ClassDetailProtocol;
import com.creative.accountsdk.net.account.TeachExitClassProtocol;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 教师
 *    班级详情
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class ClassDetailActivity extends ThirdLoginActivity {

    private  Dialog    progress;
    private  Context   context;
    private  TextView  tvInviteCode;
    private  TextView  tvInviteCount;
    private  TextView  tvCourceList;
    private  TextView  tvStudentCount;
    private  RelativeLayout  rlSelectCourse ;
    private  RelativeLayout  rlClassStudent;
    private  Button    btnExit;
    private  long classId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置无标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.teacher_class_detail);
        this.initview();
        this.initData();
        this.setListener();
    }

    private void  initview(){
        tvInviteCode = (TextView)findViewById(R.id.tv_class_invitecode);
        tvInviteCount  = (TextView)findViewById(R.id.tv_invite_count);
        tvCourceList  = (TextView)findViewById(R.id.tv_course_des);
        tvStudentCount  = (TextView)findViewById(R.id.tv_class_student_count);
        btnExit= (Button)findViewById(R.id.btn_class_exit);
        rlSelectCourse = (RelativeLayout)findViewById(R.id.rl_select_course);
        rlClassStudent = (RelativeLayout)findViewById(R.id.rl_class_students);
    }

    private void initData(){
        DataCenter.init(this);
        context = this;
        classId = this.getIntent().getLongExtra("classid",0);
        this.loadClassDetail(classId);
    }
    private void setListener(){
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendExitClass(12);
            }
        });
        rlSelectCourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toSelectCourse();
            }
        });
        rlClassStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toClassStudent();
            }
        });
    }

    /**
     * 获取当前班级信息详情
     * 1. 班级信息 2.任教信息 3. 学员名单
     */
    private void loadClassDetail(final long  classid){
        progress = DialogUtils.createProgress(context);
        progress.show();

        final ClassDetailProtocol protocol = new ClassDetailProtocol(classid);

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {
                    Log.e("Tag", "当前班级信息.." + protocol.returnMap);
                    setViewData(protocol);
                }

            }
        });
    }

    /**
     *
     * @param protocol
     */
    private void  setViewData(final ClassDetailProtocol protocol  ){
        tvInviteCode.setText(protocol.classInfor.qrcode);
        tvInviteCount.setText(protocol.classInfor.remain+"次");
        tvCourceList.setText(protocol.courseStr);
        tvStudentCount.setText(protocol.studentList.size()+"人");
    }
    /**
     * 教师退出班级
     */
    private void sendExitClass(final long  classid){
        progress = DialogUtils.createProgress(context);
        progress.show();

        final TeachExitClassProtocol protocol = new TeachExitClassProtocol(classid);

        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(context, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, context)) {
                    Log.e("Tag", "返回退出班级信息.." + protocol.returnMap);
                    // toSlectCourse(classid);
                }

            }
        });
    }

    /**
     * 班级学生名单
     */
    private void  toClassStudent( ){
        Intent  intent =new Intent(context,ClassStudentActivity.class);

        this.startActivity(intent);
    }

    /**
     * 选择任教
     */
    private void  toSelectCourse( ){
        Intent  intent =new Intent(context,SelectCourseActivity.class);
        this.startActivity(intent);
    }
}
